home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / HANGMAN.PAS < prev    next >
Pascal/Delphi Source File  |  1985-01-20  |  5KB  |  241 lines

  1. program hangman;
  2.  
  3. (*
  4.  * this program plays the game of hangman
  5.  *)
  6.  
  7. const
  8.    stringsize =  10;     (* must be the same as makehang *)
  9.    maxturns   =   7;     (* number of turns to guess word *)
  10.    backslash  = 220;     (* ascii code for backward slant *)
  11.  
  12. type
  13.    word = string[stringsize];
  14.  
  15. var
  16.  
  17.    dict      : file of word;
  18.    aword     : word;
  19.    guesses, wordsletters : set of 'A'..'Z';
  20.    numwords  : integer;
  21.    turns     : integer;
  22.    guessedit : boolean;
  23.  
  24. (*
  25.  * convert
  26.  * converts a string to the number that it represents. all non-numeric
  27.  * characters are ignored.
  28.  *)
  29.  
  30. function convert(number : word) : integer;
  31.  
  32. var
  33.    result  : integer;
  34.    strindex: integer;
  35.  
  36. begin
  37.    result := 0;
  38.    for strindex := 1 to length(aword) do
  39.       if number[strindex] in ['0'..'9'] then
  40.          result := result * 10 + ord(number[strindex]) - ord('0');
  41.    convert := result;
  42. end;
  43.  
  44. procedure initialize;
  45.  
  46. begin
  47.    clrscr;
  48.    assign(dict,'words.dat');
  49.    reset(dict);
  50.    read(dict,aword);
  51.    numwords := convert(aword);
  52.    randomize;
  53.    guesses := [];
  54.    wordsletters := [];
  55.    turns := 0;
  56.    guessedit := false;
  57. end;
  58.  
  59. (*
  60.  * this procedure randomly picks a word out of the hangman dictionary
  61.  *)
  62.  
  63. procedure chooseword;
  64.  
  65. var
  66.    nthword,i : integer;
  67.  
  68. begin
  69.    nthword := 1 + random(numwords);
  70.    seek(dict, nthword);
  71.    read(dict,aword);
  72.    for i := 1 to length(aword) do
  73.       if not (aword[i] in wordsletters) then
  74.          wordsletters := wordsletters + [aword[i]];
  75. end;
  76.  
  77. (*
  78.  * readguess :
  79.  * asks for another letter that the player expects to be in the word.
  80.  * a letter that already has been guessed is not accepted
  81.  *)
  82.  
  83. procedure readguess;
  84.  
  85. var
  86.    thisguess : char;
  87.    invalid, erase : boolean;
  88.  
  89. begin
  90.    invalid := true;
  91.    erase := false;
  92.    while invalid do
  93.       begin
  94.       gotoxy(1,10);
  95.       write('Next guess? ');
  96.       readln(thisguess);
  97.       if eof(con) then
  98.          halt
  99.       else
  100.          if thisguess in guesses then
  101.             begin
  102.             gotoxy(1,12);
  103.             write('You have already used ''',thisguess,'''.');
  104.             erase := true;
  105.             end
  106.          else
  107.             begin
  108.             guesses := guesses + [thisguess];
  109.             invalid := false;
  110.             end;
  111.       end;
  112.    if erase then
  113.       begin
  114.       gotoxy(1,12);
  115.       write(' ':27);
  116.       end;
  117.    if not (thisguess in wordsletters) then
  118.       turns := turns + 1;
  119. end;
  120.  
  121. (*
  122.  * printsofar
  123.  * procedure that prints out the word being guessed. letters that aren't
  124.  * known yet are printed as '-'. the letters that have been guessed are
  125.  * printed underneath.
  126.  *)
  127.  
  128. procedure printsofar;
  129.  
  130. var
  131.    wordindex     : integer;
  132.    guessindex    : char;
  133.    correctcount  : integer;
  134.  
  135. begin
  136.    correctcount := 0;
  137.    gotoxy(10,5);
  138.    for wordindex := 1 to length(aword) do
  139.       begin
  140.       if aword[wordindex] in guesses then
  141.          begin
  142.          write(aword[wordindex]);
  143.          correctcount := correctcount + 1;
  144.          end
  145.       else
  146.          write('-');
  147.       write(' ');
  148.       end;
  149.    gotoxy(10,7);
  150.    for guessindex := 'A' to 'Z' do
  151.       if guessindex in guesses then
  152.          write(guessindex);
  153.    if correctcount = length(aword) then
  154.       guessedit := true;
  155. end;
  156.  
  157. (*
  158.  * printhang
  159.  * prints the hangman
  160.  *)
  161.  
  162. procedure printhang;
  163.  
  164. begin
  165.    case turns of
  166.       1 : begin
  167.              gotoxy(30,5);
  168.              write('0');
  169.              end;
  170.       2 : begin
  171.              gotoxy(29,6);
  172.              write('---');
  173.              end;
  174.       3 : begin
  175.              gotoxy(28,7);
  176.              write('/');
  177.              gotoxy(27,8);
  178.              write('/');
  179.              end;
  180.       4 : begin
  181.              gotoxy(32,7);
  182.              write('\');
  183.              gotoxy(33,8);
  184.              write('\');
  185.              end;
  186.       5 : begin
  187.              gotoxy(30,7);
  188.              write('I');
  189.              gotoxy(30,8);
  190.              write('I');
  191.              end;
  192.       6 : begin
  193.              gotoxy(29,9);
  194.              write('/');
  195.              gotoxy(28,10);
  196.              write('/');
  197.              end;
  198.       7 : begin
  199.              gotoxy(31,9);
  200.              write('\');
  201.              gotoxy(32,10);
  202.              write('\');
  203.              end;
  204.    end;
  205. end;
  206.  
  207. procedure printoutcome;
  208.  
  209. begin
  210.    gotoxy(5,16);
  211.    if guessedit then
  212.       begin
  213.       writeln('Rats!! You guessed it!!');
  214.       end
  215.    else
  216.       begin
  217.       writeln('The word was : ',aword);
  218.       writeln;
  219.       writeln('Due to the violent nature of your demise, the rest of the');
  220.       writeln('scene has been censored.');
  221.       end;
  222. end;
  223.  
  224.  
  225. begin
  226.    initialize;
  227.    chooseword;
  228.    printsofar;
  229.    while not guessedit and (turns < maxturns) do
  230.       begin
  231.       readguess;
  232.       printsofar;
  233.       printhang;
  234.       end;
  235.    printoutcome;
  236. end.
  237.  
  238.  
  239.  
  240.  
  241.